/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.farsunset.lvxin.cim.push;

import com.farsunset.cim.sdk.server.model.CIMSession;
import com.farsunset.cim.sdk.server.model.Message;
import com.farsunset.lvxin.service.ApnsService;
import com.farsunset.lvxin.service.CIMSessionService;
import com.farsunset.lvxin.service.IDGenerateService;
import com.farsunset.lvxin.service.MessageService;
import com.farsunset.lvxin.util.MessageUtil;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

public abstract class BaseMessagePusher {

	@Value("#{'${server.host}'.trim()}")
	private String host;

	@Resource
	private ApnsService apnsService;

	@Resource
	protected MessageService messageService;

	@Resource
	protected CIMSessionService cimSessionService;

	@Resource
	private IDGenerateService snowflakeService;

	/**
	 * 消息发送之前的预处理
	 * @param message
	 */
	public  void preHandle(Message message){}

	/**
	 * 消息发送之后的预处理
	 * @param message
	 */
	public  void postHandle(Message message){}


	/**
	 * 向用户发送消息
	 * 
	 * @param message
	 */
	public void push(Message message, CIMSession session) {
		
		if(session == null) {
			return;
		}

		/*
		 * IOS设备，如果开启了apns，则使用apns推送
		 */
		if (session.isIOSChannel() && session.isApnsOpend()) {
			apnsService.push(message, session.getDeviceId());
			return;
		}

		/*
		 * 如果是Android，浏览器或者windows客户端则直接发送
		 */
		if (session.isConnected() && Objects.equals(host, session.getHost())) {
			session.write(message);
		}
	}



	protected void push(Message message) {
		CIMSession session = cimSessionService.get(message.getReceiver());
		this.push(message, session);
	}

	public void push(Message msg,List<String> accountList) {

		List<Message> messageList = new LinkedList<>();
		for (String account : accountList) {

			Message message = MessageUtil.clone(msg);
			message.setReceiver(account);
			message.setId(snowflakeService.getLongId());

			messageList.add(message);

			this.push(message);
		}
		messageService.save(messageList);

	}


}
